package com.tiyende.credit.loop.common

import android.view.View
import com.tiyende.credit.loop.TiyendeApp
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.base.BaseActivity
import com.tiyende.credit.loop.event.TiyendeEventData
import com.tiyende.credit.loop.event.TiyendeEventPointHelper
import com.tiyende.credit.loop.event.TiyendeEventSubscriber
import com.tiyende.credit.loop.request.HttpHelper
import com.tiyende.credit.loop.request.IHttpCallback
import com.tiyende.credit.loop.ui.dialog.TiyendeCustomerDialog
import com.tiyende.credit.loop.ui.dialog.TiyendeTipsDialog
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import org.json.JSONObject

/**
 * CommonActivity
 * @Author wwdeng
 * @CreateTime: 2024/11/20 10:32
 * @Annotation：
 */
abstract class CommonActivity: BaseActivity(), TiyendeEventSubscriber,ICommonView, CoroutineScope by MainScope() {

    private var errorDialog: TiyendeTipsDialog? = null

    override fun getAct(): CommonActivity {
        return this
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.setTag(this)
        TiyendeApp.app.publisher.register(this)
    }

    /**
     * 错误提示
     */
    fun showErrorTipsDialog(tips: String,eventRemark: String = ""){
        if (isFinishing || isDestroyed){
            return
        }
        if (errorDialog == null){
            errorDialog = TiyendeTipsDialog(this,eventRemark)
        }
        errorDialog?.let {
            if (it.isShowing){
                return
            }
        }
        errorDialog?.setOnClick(object : TiyendeTipsDialog.IDialogCallback{
            override fun onClick(view: View?) {

            }
        })
        errorDialog?.show()
        errorDialog?.isTiyendeDoubleButton(false)
        errorDialog?.addTiyendeTipsText(tips)
    }

    /**
     * 客服
     */
    fun showCustomerDialog(eventRemark: String = "",pName: String = "", pToken: String = ""){
        TiyendeCustomerDialog(this)
            .setCustomerCancelable(true)
            .setCustomerCanceledOnTouchOutside(true)
            .setCustomerProductName(pName)
            .setCustomerProductToken(pToken)
            .setCustomerEventRemark(eventRemark)
            .setCustomerClickCallback(object : TiyendeCustomerDialog.IDialogCallback{
                override fun onClick(view: View?) {
                }
            })
            .show()
    }

    /**
     * 接口请求失败提示
     */
    fun showRequestErrorDialog(){
        if (isFinishing || isDestroyed){
            return
        }
        if (errorDialog == null){
            errorDialog = TiyendeTipsDialog(this)
        }
        errorDialog?.let {
            if (it.isShowing){
                return
            }
        }
        errorDialog?.setOnClick(object : TiyendeTipsDialog.IDialogCallback{
            override fun onClick(view: View?) {
                if (view?.id == R.id.btTipsOK){
                    requestTryAgain()
                }
            }
        })
        errorDialog?.addTiyendeTipsEventRemark("network error")
        errorDialog?.setCancelable(true)
        errorDialog?.setCanceledOnTouchOutside(false)
        errorDialog?.show()
        errorDialog?.isTiyendeDoubleButton(true)
        errorDialog?.addTiyendeBtnOkText(getString(R.string.tiyende_button_try_again))
        errorDialog?.addTiyendeTipsText(getString(R.string.tiyende_dialog_network_error))
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String,params: JSONObject,callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,callback)
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String? = "") {
        TiyendeEventPointHelper.addEventPointRequestAction(eventName,eventRemark)
    }

    /**
     * 发送事件通知
     */
    fun publishEventData(eventData: TiyendeEventData){
        TiyendeApp.app.publisher.publish(eventData)
    }

    /**
     * 处理订阅事件
     */
    override fun onTiyendePublishEvent(event: TiyendeEventData) {
    }

    /**
     * 重试
     */
    override fun requestTryAgain() {
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.cancelTag(this)
        TiyendeApp.app.publisher.detach(this)
    }
}